/*
 * Copyright (C) 2021 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xuidemo.fragment.diandiandian;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.baidu.aip.asrwakeup3.core.recog.MyRecognizer;
import com.baidu.aip.asrwakeup3.core.wakeup.MyWakeup;
import com.baidu.aip.asrwakeup3.core.wakeup.WakeUpResult;
import com.baidu.aip.asrwakeup3.core.wakeup.listener.IWakeupListener;
import com.baidu.speech.EventManager;
import com.baidu.speech.asr.SpeechConstant;
import com.xuexiang.xaop.annotation.SingleClick;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.button.roundbutton.RoundButton;
import com.xuexiang.xui.widget.button.roundbutton.RoundDrawable;
import com.xuexiang.xui.widget.layout.XUILinearLayout;
import com.xuexiang.xuidemo.R;
import com.xuexiang.xuidemo.base.BaseFragment;
import com.xuexiang.xuidemo.fragment.zhuanquanquan.DecisionFragment;
import com.xuexiang.xuidemo.view.FingerTouch;

import java.util.HashMap;
import java.util.Map;

@Page(name = "FingerTouch", anim = CoreAnim.none)
public class FingerTouchFragment extends BaseFragment {
    private FingerTouch fingerTouch;
    private RoundButton button;
    private EventManager asr;
    private MyRecognizer rec;
    protected MyWakeup myWakeup;
    private IWakeupListener listener = new IWakeupListener() {
        @Override
        public void onSuccess(String word, WakeUpResult result) {
            fingerTouch.Select();
        }

        @Override
        public void onStop() {

        }

        @Override
        public void onError(int errorCode, String errorMessge, WakeUpResult result) {

        }

        @Override
        public void onASrAudio(byte[] data, int offset, int length) {

        }
    };

    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle();
        titleBar.setLeftImageResource(R.drawable.smallsmallpoint);
        titleBar.setLeftClickListener(new View.OnClickListener() {
            @Override
            @SingleClick
            public void onClick(View v) {
                //Toast.makeText(getContext(), "SmallPoint", Toast.LENGTH_SHORT).show();
            }
        });
        return titleBar;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_finger_touch;
    }

    @Override
    protected void initViews() {
        fingerTouch = (FingerTouch) findViewById(R.id.finger_touch);
        button = (RoundButton) findViewById(R.id.finger_touch_btn);
        RoundDrawable drawable = (RoundDrawable) button.getBackground();
        drawable.setColor(Color.parseColor("#1296db"));
        XUILinearLayout layout = (XUILinearLayout) findViewById(R.id.xui_layout01);
        layout.setRadiusAndShadow(DensityUtils.dp2px(getContext(), 38), 14, 0.75f);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fingerTouch.Select();
            }
        });
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(SpeechConstant.WP_WORDS_FILE, "assets:///WakeUp.bin");
        myWakeup = new MyWakeup(getContext(), listener);
        myWakeup.start(params);
    }

    @Override
    public void onDestroyView() {
        myWakeup.release();
        super.onDestroyView();
    }
}
